/**
 * PushService.java 
 * Copyright © 2009-2015, 杭州泊享网络科技有限公司
 * 
 * @author Liu Tao
 * @create 2015年3月15日
 */
package com.pshare.msg.push.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pshare.appdevice.bean.AppDevice;
import com.pshare.appdevice.service.AppDeviceService;
import com.pshare.constant.SeaxConstants;
import com.pshare.enums.OsType;
import com.pshare.service.AbstractPushService;

/**
 * 推送Service层接口
 */
@Service
public class PushService extends AbstractPushService {

	private static final Logger logger = Logger.getLogger(PushService.class);

	@Autowired
	private AppDeviceService deviceService;

	/**
	 * 推送给单个用户
	 */
	protected void doPush(Long userId, String title, String message, String objId, Integer bizType, String sound) {
		// 允许一个账号同时登录多个客户端
		List<AppDevice> devices = deviceService.getDevice(userId, getAppType());
		if (devices == null || devices.size() == 0) {
			return;
		}

		sendToDeviceList(devices, title, message, objId, bizType, sound);
	}

	/**
	 * 推送给一组用户
	 */
	protected void doPush(List<Long> userIds, String title, String message, String objId, Integer bizType, String sound) {
		List<AppDevice> devices = deviceService.getDeviceByUserIds(userIds, getAppType());
		if (devices == null || devices.size() == 0) {
			return;
		}

		sendToDeviceList(devices, title, message, objId, bizType, sound);
	}

	/**
	 * @param title 推送标题
	 * @param message 内容
	 * @param bizType 消息的业务逻辑类型
	 * @param devices 设备标识
	 */
	protected void sendToDeviceList(List<AppDevice> devices, String title, String message, String objId,
			Integer bizType, String sound) {
		List<String> iosTokenList = new ArrayList<String>();
		List<String> pureIosTokenList = new ArrayList<String>();
		List<String> androidTokenList = new ArrayList<String>();
		for (AppDevice device : devices) {
			Integer osType = device.getOsType();
			String deviceToken = device.getDeviceToken();

			if (StringUtils.isBlank(deviceToken) || SeaxConstants.NULL_VALUE.equalsIgnoreCase(deviceToken)) {
				continue;
			}
			if (osType == OsType.IOS.getCode()) { // iOS
				if (deviceToken.length() != IOS_LENGTH) {
					continue;
				}
				if (bizType == 99) { // 退出信息不用角标
					iosTokenList.add(deviceToken);
				} else {
					iosTokenList.add(deviceToken + SeaxConstants.HYPHEN + device.getUnreadCount()); // iOS需要未读条数角标,"deviceToken-count"格式
					pureIosTokenList.add(deviceToken);
				}
			} else if (osType == OsType.ANDROID.getCode()) { // Android
				// 长度校验
				if (lenthCheck != null && deviceToken.length() != lenthCheck) {
					continue;
				}
				androidTokenList.add(deviceToken);
			}
		}

		// iOS推送
		if (iosTokenList.size() > 0) {
			try {
				apnsPush(message, objId, iosTokenList, bizType, sound);
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
			if (pureIosTokenList.size() > 0) {
				deviceService.addUnreadCount(pureIosTokenList, 1); // 未读条数+1
			}
		}

		// Android推送
		if (androidTokenList.size() > 0) {
			getAndroidPushSender().doPush(2, androidTokenList, title, message, objId, bizType, sound);
		}

	}

	/**
	 * 广播
	 * 
	 * @param message
	 * @param msgType
	 */
	protected void broadcast(String title, String message, String objId, Integer msgType, String sound) {
		List<AppDevice> allIosDeviceList = deviceService.getDeviceList(OsType.IOS.getCode(), getAppType());
		List<String> iosTokenList = new ArrayList<String>();
		List<String> pureIosTokenList = new ArrayList<String>();
		for (AppDevice device : allIosDeviceList) {
			String deviceToken = device.getDeviceToken();

			if (StringUtils.isBlank(deviceToken) || SeaxConstants.NULL_VALUE.equalsIgnoreCase(deviceToken)
					|| deviceToken.length() != IOS_LENGTH) {
				continue;
			}

			iosTokenList.add(deviceToken + SeaxConstants.HYPHEN + device.getUnreadCount()); // iOS需要未读条数角标
			pureIosTokenList.add(deviceToken);
		}

		// iOS推送
		if (iosTokenList.size() > 0) {
			try {
				apnsPush(message, objId, iosTokenList, msgType, sound);
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
			deviceService.addUnreadCount(pureIosTokenList, 1); // 未读条数+1
		}
		// Android推送
		try {
			getAndroidPushSender().doPush(3, null, title, message, objId, msgType, sound);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

}
